using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CMS.Application.Common.Interface;
using CMS.Domain.Entity;
using Microsoft.AspNetCore.Mvc;

namespace CMS.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class CommentController : ControllerBase
    {
        //评论表
        private readonly IRepository<Comment> _Comment;
        //用户表
        private readonly IRepository<Users> _Users;
        //博文表
        private readonly IRepository<Blog> _Blog;
        // private readonly IRepository<Comment> _Comment; 

        public CommentController(IRepository<Comment> Comment, IRepository<Users> Users, IRepository<Blog> Blog)
        {
            _Comment = Comment;
            _Users = Users;
            _Blog = Blog;
        }
        //要返回：评论id，文章id，文章标题 ，评论者(名字)，评论内容,日期，

        //所有的评论(管理员查看所有评论)
        [HttpGet("/getComment/{_page}")]
        public async Task<Array> getComment(int _page)
        {
            
            var comment = _Comment.Table.Select(x => new { x.Id,x.CommentId, x.BlogId,x.CommentText, x.UpdatedAt, x.UserId }).ToList();
            object[] arr = new object[comment.Count()];//长度为评论长度的object数组
            List<string> UserName = new List<string>();
            List<string> BlogName = new List<string>();
            
            for (int i = 0; i < comment.Count(); i++)
            {
                var user=_Users.Table.Select(x => new { x.Id, x.UserName }).Where(x => x.Id == comment[i].UserId).ToList();
                //根据评论表UpdatedBy查找写评论用户名
                //  UserName = _Users.Table.Select(x => new { x.Id, x.UserName }).Where(x => x.Id == comment[i].UpdatedBy);
                
                if (UserName.Count()==0)
                {
                    UserName.Add("匿名");
                }else{
                    UserName.Add(user[0].UserName);
                }
                
                //根据评论表BlogId查找博文标题
                var BgName=_Blog.Table.Select(x => new { x.Id, x.BlogTitle }).Where(x => x.Id == comment[i].BlogId).ToList();
                
                if (BgName.Count()==0)
                {
                    BlogName.Add("已删除");
                }else{

                    BlogName.Add(BgName[0].BlogTitle);
                }
                
            }

            
            object[] commentData=new object[comment.Count()];

            for (int i = 0; i < comment.Count(); i++)
            {
                //要返回：评论id，文章id，文章标题 ，评论者(名字)，评论内容,日期，
                commentData[i]=new {
                    Id=comment[i].Id,
                    CommentId=comment[i].CommentId,
                    BlogId=comment[i].BlogId,
                    BlogTitle=BlogName[i],
                    UsName=UserName[i],
                    CommentText=comment[i].CommentText,
                    UpdatedAt=comment[i].UpdatedAt,
                    count=comment.Count()
                };
            }
            var newblogs = commentData.Skip((_page-1) * 10).Take(10).ToArray();
            

            return newblogs;
        }


        //根据博文id找评论
        [HttpGet("/getCommen/{BlogId}")]
        public async Task<object> getCommentById(Guid BlogId)
        {
            var comment = _Comment.Table.Select(x => new { x.Id,x.CommentId,x.UserId, x.BlogId,x.CommentText, x.UpdatedAt, x.UpdatedBy })
            .Where(x=>x.BlogId==BlogId).ToList();
            //获得这边博客的所有评论

            object[] arr = new object[comment.Count()];//长度为评论长度的object数组
            List<string> UserName = new List<string>();
            List<string> BlogName = new List<string>();
            
            for (int i = 0; i < comment.Count(); i++)
            {
                var user=_Users.Table.Select(x => new { x.Id, x.UserName }).Where(x => x.Id == comment[i].UserId).ToList();
                //根据评论表UpdatedBy查找写评论用户名
                //  UserName = _Users.Table.Select(x => new { x.Id, x.UserName }).Where(x => x.Id == comment[i].UpdatedBy);
                if (UserName.Count()==0)
                {
                    UserName.Add("匿名");
                }else{
                    UserName.Add(user[0].UserName);
                }
                //管家评论表BlogId查找博文标题
                var BgName=_Blog.Table.Select(x=>new {x.Id,x.BlogTitle}).Where(x=>x.Id==comment[i].BlogId).ToList();
                BlogName.Add(BgName[0].BlogTitle);
            }
            object[] commentData=new object[comment.Count()];

            for (int i = 0; i < comment.Count(); i++)
            {
                //要返回：评论id，文章id，文章标题 ，评论者(名字)，评论内容,日期，
                commentData[i]=new {
                    Id=comment[i].Id,
                    CommentId=comment[i].CommentId,
                    BlogId=comment[i].BlogId,
                    BlogTitle=BlogName[i],
                    UsName=UserName[i],
                    CommentText=comment[i].CommentText,
                    UpdatedAt=comment[i].UpdatedAt,
                    count=comment.Count()
                };
            }
            return commentData;
        }


    }
}